<?php

/**
 * cart actions.
 *
 * @package    emc
 * @subpackage cart
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class cartActions extends sfActions
{
	/**
	* Executes index action
	*
	* @param sfRequest $request A request object
	*/
	public function executeIndex(sfWebRequest $request)
	{
		$this->forward('default', 'module');
	}
	
	public function executeAdd(sfWebRequest $request)
	{
		$product_id	= $request->getParameter("product_id");
		$option_id	= $request->getParameter("option_id");
		$option_id += 0;
		$quantity	= $request->getParameter("quantity");
		
		if ($this->getUser()->hasAttribute("cart"))
			$cart = $this->getUser()->getAttribute("cart");
		else 
			$cart = array();
		
		$is_exist = false;
		foreach ($cart as $key=>$cart_item)
		{
			if ($cart_item['product_id'] == $product_id && $cart_item['option_id'] == $option_id)
			{
				$cart[$key]['quantity'] = $quantity;
				$is_exist = true;
			}
		}
		
		if (!$is_exist)
		{
			$item_to_add = array();
			$item_to_add['product_id'] = $product_id;
			$item_to_add['option_id'] = $option_id;
			$item_to_add['quantity'] = $quantity;
			$cart[] = $item_to_add;
		}
		$this->cart_quantity = count($cart);
		$this->getUser()->setAttribute("cart", $cart);
		$this->setTemplate("add");
	  	$this->setLayout(false);
	}
	
	public function executeDelete(sfWebRequest $request)
	{
		$product_id = $request->getParameter("product_id");
		$option_id = $request->getParameter("option_id");
		if ($this->getUser()->hasAttribute("cart"))
			$cart = $this->getUser()->getAttribute("cart");
		else 
			$cart = array();
		
		$totalPrice = 0;
		foreach ($cart as $key=>$cart_item)
		{
			if ($cart_item['product_id'] == $product_id && $cart_item['option_id'] == $option_id)
				unset($cart[$key]);
			else
			{
				$product = Doctrine_Core::getTable("Product")->find($cart_item['product_id']);
				$totalPrice += (int)$product->discount_price * $cart_item['quantity'];
			}
		}
		
		$this->totalPrice = $totalPrice;
		
		$this->getUser()->setAttribute("cart", $cart);
		$this->setTemplate("delete");
	  	$this->setLayout(false);
	}
	
	public function executeGetQuantity(sfWebRequest $request)
	{
		if ($this->getUser()->hasAttribute("cart"))
			$cart = $this->getUser()->getAttribute("cart");
		else 
			$cart = array();
		
		$this->cart_quantity = sizeof($cart);
		
		$this->getUser()->setAttribute("cart", $cart);
		$this->setTemplate("getQuantity");
	  	$this->setLayout(false);
	}
	
	public function executeList(sfWebRequest $request)
	{
		if ($this->getUser()->hasAttribute("cart"))
			$cart = $this->getUser()->getAttribute("cart");
		else 
			$cart = array();
		
		$cart_list = $cart;
		foreach ($cart_list as $key=>$cart_item)
		{
			$product = Doctrine_Core::getTable("Product")->find($cart_item['product_id']);
			$option = Doctrine_Core::getTable("ProductOption")->find($cart_item['option_id']);
			$cart_list[$key]["product"] = $product;
			$cart_list[$key]["option"] = $option;
		}
		
		$this->cart_list = $cart_list;
		
		$this->getUser()->setAttribute("cart", $cart);
		$this->setTemplate("list");
	  	$this->setLayout(false);
	}
}
